<template>
  <div class="dashboard-container">
    <div class="left">
      <el-timeline class="padding0">
        <el-timeline-item>
          <el-card class="box-card">
            <el-link :underline="false" class="linkFont" @click="getAllColleges">查看所有数据 &nbsp;&nbsp;<svg-icon icon-class="eye-open" /></el-link>
          </el-card>
        </el-timeline-item>
        <el-timeline-item>
          <el-card class="box-card">
            <el-link :underline="false" class="linkFont" @click="dialogFormVisible = true">新增院系信息 &nbsp;&nbsp;<i class="el-icon-circle-plus-outline" /></el-link>
          </el-card>
        </el-timeline-item>
        <el-timeline-item>
          <el-card class="box-card">
            <el-link :underline="false" class="linkFont">查找院系 &nbsp;&nbsp;<i class="el-icon-search" /></el-link>
            <el-form ref="searchForm" :model="searchForm" label-width="80px" inline label-position="top" :rules="rules1">
              <el-form-item label="根据院系名称查找" prop="collegeName">
                <el-input v-model="searchForm.collegeName" style="width: 60%" placeholder="输入院系名称" />
                <el-button type="primary" icon="el-icon-search" style="float: right" @click="searchCollege"> 搜索</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </div>
    <div class="right">
      <el-table
        v-loading="listLoading"
        :data="collegeList"
        border
        height="600"
        stripe
        style="width: 100%"
      >
        <el-table-column
          prop="collegeId"
          label="院系编号"
        />
        <el-table-column
          prop="name"
          label="院系名称"
          align="center"
        />
        <el-table-column
          prop="collegeOfficer"
          label="院系主管"
          align="center"
        />
        <el-table-column
          prop="location"
          label="地址"
        />
        <el-table-column
          prop="peopleNumber"
          label="院系人数"
          align="center"
        />
        <el-table-column
          width="170"
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-popconfirm
              title="确定删除吗？"
              @onConfirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button
                size="mini"
                type="danger"
                slot="reference"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="新增院系信息" :visible.sync="dialogFormVisible">
      <el-form ref="myForm" :model="editableCollege" :rules="rules2" label-width="80px" label-position="left" class="editForm">
        <el-form-item label="院系编号" prop="collegeId">
          <el-input v-model="editableCollege.collegeId" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="院系名称" prop="name">
          <el-input v-model="editableCollege.name" />
        </el-form-item>
        <el-form-item label="院系主管" prop="collegeOfficer">
          <el-input v-model="editableCollege.collegeOfficer" />
        </el-form-item>
        <el-form-item label="地址" prop="location">
          <el-input v-model="editableCollege.location" />
        </el-form-item>
        <el-form-item label="院系人数" prop="peopleNumber">
          <el-input-number v-model="editableCollege.peopleNumber" :min="0" label="描述文字" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="confirmForm('myForm')">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { findAllCollege, selectCollegeByName, deleteCollege, updateCollegeById, addCollege } from '@/api/college'
export default {
  name: 'Dashboard',
  data() {
    return {
      collegeList: [{
        collegeId: 'f',
        name: 'dgfg',
        collegeOfficer: 'ywj',
        location: 'ywj',
        peopleNumber: 9999
      }],
      editableCollege: {},
      searchForm: {
        collegeName: ''
      },
      dialogFormVisible: false,
      isEdit: false,
      listLoading: false,
      rules1: {
        collegeName: [
          { required: true, message: '请输入学院名称', trigger: 'blur' }
        ]
      },
      rules2: {
        collegeId: [
          { required: true, message: '请输入院系编号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入院系名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        collegeOfficer: [
          { required: true, message: '请输入院系主管', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入院系地址', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        peopleNumber: [
          { type: 'integer', required: true, message: '请输入院系人数且为整数', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  watch: {
    dialogFormVisible(n, o) {
      if (!n) {
        this.editableCollege = {}
        this.isEdit = false
      }
    }
  },
  created() {
    this.getAllColleges()
  },
  methods: {
    getAllColleges() {
      this.listLoading = true
      findAllCollege().then(res => {
        this.dialogFormVisible = false
        this.collegeList = res.data.colleges
        this.listLoading = false
      })
    },
    confirmForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            this.updateCollege()
          } else {
            this.addNewCollege()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    updateCollege() {
      updateCollegeById(this.editableCollege).then(res => {
        this.getAllColleges()
        this.$message({
          showClose: true,
          message: '操作成功',
          type: 'success'
        })
      })
    },
    addNewCollege() {
      addCollege(this.editableCollege).then(res => {
        this.getAllColleges()
        this.$message({
          showClose: true,
          message: '操作成功',
          type: 'success'
        })
      })
    },
    searchCollege() {
      this.$refs.searchForm.validate((valid) => {
        if (valid) {
          this.listLoading = true
          selectCollegeByName({ name: this.searchForm.collegeName }).then(res => {
            this.collegeList = res.data.college
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleEdit(index, row) {
      this.editableCollege = row
      this.isEdit = true
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {
      deleteCollege({ id: row.collegeId }).then(res => {
        this.getAllColleges()
        this.$message({
          showClose: true,
          message: '操作成功',
          type: 'success'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 10px;
    min-width: 1280px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
  .left{
    width: 23%;
    min-width: 330px;
    text-align: center;
    float: left;
    font-weight: 700;
  }
  .right{
    float: left;
    margin-left: 3%;
    width: 71%;
  }
  .padding0{
    padding: 0;
  }
  .linkFont{
    font-weight: 700;
  }
  .editForm{
    width: 400px;
  }
  /deep/
  .el-form-item__label{
    float: left;
    font-weight: 500;
  }
</style>
